/*
 * @Author: webcc
 * @Date: 2022-10-22 16:27:44
 * @LastEditTime: 2022-10-24 00:20:44
 * @email: webcc.coder@qq.com
 */
import { message } from 'antd'
import axios from 'axios'
import { hasToken, getToken, removeToken } from './useToken'
import history from 'utils/history'
export const baseURL = 'http://geek.itheima.net/v1_0/'
const instance = axios.create({
    baseURL,
    timeout: 5000
})

instance.interceptors.request.use(
    function (config) {
        if (hasToken()) {
            config.headers.Authorization = `Bearer ${getToken()}`
        }
        return config
    }
)

instance.interceptors.response.use(
    function (res) {
        return res.data
    },
    function (err) {
        if (!err.response) {
            // 请求超时
            message.error("网络繁忙")
            return Promise.reject("网络繁忙")
        }
        // 配置token过期处理
        if (err.response.status == 401) {
            message.warn("登录信息过期，请重新登录")
            removeToken();
            // 跳转
            history.push('/login')
        }
    }
)

export default instance